package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.FarmerCropDetailActivity;
import com.hemaapp.yjnh.listener.OrderItemClickListener;
import com.hemaapp.yjnh.bean.FarmerReturnOrder;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.math.BigDecimal;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类描述：田家问题订单适配器
 * 创建人：GuiyouZhang
 * 创建时间：2016/7/20 17:22
 * 修改备注：
 */
public class FarmerReturnOrderListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<FarmerReturnOrder> datas;
    private Context mContext;
    private OrderItemClickListener clickListener;

    public FarmerReturnOrderListAdapter(Context context, ArrayList<FarmerReturnOrder> orders, OrderItemClickListener clickListener) {
        this.mContext = context;
        this.datas = orders;
        this.clickListener = clickListener;
    }

    @Override
    public int getItemViewType(int position) {
        if (datas.size() == 0)
            return -1;
        else
            return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == -1) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.listitem_empty_base, parent, false);
            return new EmptyViewHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_farmer_return_order, parent, false);
            return new ViewHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (datas.size() == 0) {
            if (holder instanceof EmptyViewHolder)
                ((EmptyViewHolder) holder).tvEmtpy.setText("暂时没有订单");
        } else {
            if (holder instanceof ViewHolder) {
                ((ViewHolder) holder).setDatas(position);
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas.size() == 0 ? 1 : datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_order_num)
        TextView tvOrderNum;
        @Bind(R.id.tv_status)
        TextView tvStatus;
        @Bind(R.id.layout_production)
        LinearLayout layoutProduction;
        @Bind(R.id.iv_pic)
        ImageView ivPic;
        @Bind(R.id.tv_good_name)
        TextView tvGoodName;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_count)
        TextView tvCount;
        @Bind(R.id.tv_commit)
        TextView tvCommit;
        @Bind(R.id.tv_aftersale)
        TextView tvAftersale;
        @Bind(R.id.tv_hint)
        TextView tvHint;
        @Bind(R.id.tv_freight)
        TextView tvFreight;
        @Bind(R.id.tv_total_fee)
        TextView tvTotalFee;
        @Bind(R.id.tv_refuse_return)
        TextView tvRefuseReturn;
        @Bind(R.id.tv_agree_return)
        TextView tvAgreeReturn;
        @Bind(R.id.tv_confirm)
        TextView tvConfirm;
        int position = -1;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null && position != -1)
                        clickListener.onItemClickListener(position);
                }
            });
            layoutProduction.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent it = new Intent(mContext, FarmerCropDetailActivity.class);
                    it.putExtra("blog_id", datas.get(position).getBlog_id());
                    mContext.startActivity(it);
                }
            });
            tvRefuseReturn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null && position != -1)
                        clickListener.onRefuseReturn(position);
                }
            });
            tvAgreeReturn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null && position != -1)
                        clickListener.onAgreeReturn(position);
                }
            });
            tvConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null && position != -1)
                        clickListener.onReceive(position);
                }
            });
        }

        public void setDatas(int position) {
            this.position = position;
            FarmerReturnOrder order = datas.get(position);
            double price = Double.parseDouble(order.getPrice());
            int buyCount = Integer.parseInt(order.getBuycount());

            //根据订单状态来显示下方的操作按钮
            tvOrderNum.setText("订单号：" + order.getBill_sn());
            tvStatus.setText(getTradeStatus(order));
            tvFreight.setText("合计(含运费¥" + uuUtils.formatAfterDot2(order.getShipping_fee()) + ")：");
            ImageUtils.loadSmalImage(mContext, order.getImgurl(), ivPic);
            tvGoodName.setText(order.getName());
            tvPrice.setText("¥" + uuUtils.formatAfterDot2(order.getPrice()));
            tvCount.setText("x"+order.getBuycount());
            tvTotalFee.setText(uuUtils.formatAfterDot2(price*buyCount+""));
//            BigDecimal bg = new BigDecimal(price*buyCount);
//            tvTotalFee.setText("¥"+bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        }

        /**
         * 获取订单的状态
         *
         * @param order
         * @return
         */
        private String getTradeStatus(FarmerReturnOrder order) {
            tvRefuseReturn.setVisibility(View.GONE);
            tvAgreeReturn.setVisibility(View.GONE);
            tvConfirm.setVisibility(View.GONE);
            //0正常;1申请中;2等待买家发货;3拒绝;4退货中;5退货完成
            String itemtype = order.getItemtype();
            if (itemtype.equals("1")) {
                tvRefuseReturn.setVisibility(View.VISIBLE);
                tvAgreeReturn.setVisibility(View.VISIBLE);
                return "待处理";
            }else if(itemtype.equals("2")) {
                return "待退货";
            }else if(itemtype.equals("3")) {
                return "拒绝退款";
            }else if (itemtype.equals("4")) {
                tvConfirm.setVisibility(View.VISIBLE);
                return "待收货";
            }else if (itemtype.equals("5")) {
                return "退款完成";
            }else
                return "未知";
        }
    }


}
